package com.xkw.autocode.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xkw.autocode.mapper.SystemLogsMapper;
import com.xkw.autocode.model.SystemLogs;
import com.xkw.autocode.service.SystemLogsService;
import com.xkw.autocode.util.ExampleUtils;
import com.xkw.autocode.util.ObjectCopyUtils;
import com.xkw.autocode.util.PageVo;
import com.xkw.autocode.vo.SystemLogsVo;

import tk.mybatis.mapper.entity.Example;

/**
 * “系统操作日志表” 业务类
 * 
 * @author auto
 */
@Service("systemLogsService")
public class SystemLogsServiceImpl implements SystemLogsService {

	private static final Logger logger = LoggerFactory.getLogger(SystemLogsServiceImpl.class);

	@Autowired
	private SystemLogsMapper systemLogsMapper;

	@Override
	public int insert(SystemLogsVo systemLogsVo) {
		if (systemLogsVo == null) {
			return 0;
		}
		SystemLogs systemLogs = this.getSystemLogs(systemLogsVo);
		if (systemLogs == null) {
			return 0;
		}

		return this.systemLogsMapper.insertSelective(systemLogs);
	}

	@Override
	public int delete(Long id) {
		if (id == null || id <= 0) {
			return 0;
		}
		return this.systemLogsMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int update(SystemLogsVo systemLogsVo) {
		if (systemLogsVo == null) {
			return 0;
		}
		SystemLogs systemLogs = this.getSystemLogs(systemLogsVo);
		if (systemLogs == null) {
			return 0;
		}

		return this.systemLogsMapper.updateByPrimaryKeySelective(systemLogs);
	}

	@Override
	public SystemLogsVo findById(Long id) {
		if (id == null || id <= 0) {
			return null;
		}

		SystemLogs systemLogs = this.systemLogsMapper.selectByPrimaryKey(id);

		try {
			return (SystemLogsVo) ObjectCopyUtils.copyProperties(systemLogs, SystemLogsVo.class);
		} catch (ReflectiveOperationException e) {
			logger.error("", e);
			return null;
		}
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public PageInfo<SystemLogsVo> findByPage(SystemLogsVo systemLogsVo, PageVo pageVo) {
		SystemLogs systemLogs = this.getSystemLogs(systemLogsVo);
		if (systemLogs == null) {
			systemLogs = new SystemLogs();
		}
		PageHelper.startPage(pageVo.getPageNo(), pageVo.getPageSize());
		if (pageVo.getOrderBy() != null && pageVo.getOrderBy().length() > 0) {
			PageHelper.orderBy(pageVo.getOrderBy());
		}

		Example example = ExampleUtils.getExampleFromObject(systemLogs);

		List<SystemLogs> list = this.systemLogsMapper.selectByExample(example);
		try {
			PageInfo pageInfo = new PageInfo<>(list);
			pageInfo.setList(ObjectCopyUtils.copyPropertiesList(list, SystemLogsVo.class));
			return pageInfo;
		} catch (ReflectiveOperationException e) {
			logger.error("", e);
			return null;
		}
	}

	private SystemLogs getSystemLogs(SystemLogsVo systemLogsVo) {
		try {
			return (SystemLogs) ObjectCopyUtils.copyProperties(systemLogsVo, SystemLogs.class);
		} catch (ReflectiveOperationException e) {
			logger.error("", e);
			return null;
		}
	}
}
